package com.tosmart.jorm.cases.oralce;

import com.tosmart.jorm.models.oracle.PersonSequenceGenId;
import com.tosmart.jorm.cases.TestBase;
import com.tosmart.jorm.util.DBAssistant;
import org.testng.Assert;
import org.testng.annotations.*;

import java.sql.SQLException;
import java.util.List;

/**
 * @author tosmart@gmail.com
 */
public class NumGenUseSeq extends TestBase {

    public static final String CREATE_SEQ_SQL =
            "CREATE SEQUENCE SEQ_PERSON_ID " +
                    "INCREMENT BY 1 " +
                    "START WITH 1 " +
                    "NOMAXVALUE " +
                    "NOCYCLE " +
                    "CACHE 10";
    public static final String DROP_SEQ_SQL =
            "DROP SEQUENCE SEQ_PERSON_ID";

    @BeforeTest
    public void setUp() throws SQLException {
        super.setUp();
        new DBAssistant(connection, false).execute(CREATE_SEQ_SQL);
    }

    @AfterTest
    public void tearDown() throws SQLException {
        new DBAssistant(connection, false).execute(DROP_SEQ_SQL);
        super.tearDown();
    }

    @Test
    public void test() {

        PersonSequenceGenId tosmart = new PersonSequenceGenId();
        tosmart.setName("tosmart");
        tosmart.setSex("M");
        tosmart.setBirth(date("1980-10-22"));
        tosmart.setFamilyId(1);

        jormFace.insertEntity(tosmart);

        PersonSequenceGenId purong = new PersonSequenceGenId();
        purong.setName("purong");
        purong.setSex("F");
        purong.setBirth(date("1978-09-17"));
        purong.setFamilyId(1);

        jormFace.insertEntity(purong);

        // assert

        List<PersonSequenceGenId> list =
                jormFace.listEntities(PersonSequenceGenId.class);
        Assert.assertEquals(list.size(), 2);

        tosmart = list.get(0);
        purong = list.get(1);

        Assert.assertTrue(tosmart.getId() < purong.getId());

        System.out.println("tosmart.getId() = " + tosmart.getId());
        System.out.println("purong.getId() = " + purong.getId());

        Assert.assertEquals(tosmart.getName(), "tosmart");
        Assert.assertEquals(purong.getName(), "purong");

        Assert.assertEquals(tosmart.getSex(), "M");
        Assert.assertEquals(purong.getSex(), "F");

        Assert.assertEquals(tosmart.getBirth(), date("1980-10-22"));
        Assert.assertEquals(purong.getBirth(), date("1978-09-17"));

        Assert.assertEquals(tosmart.getFamilyId(), 1);
        Assert.assertEquals(purong.getFamilyId(), 1);
    }
}